package com.wy.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Time:8/2/21
 * Author:ypf
 * Description:
 */
public class MyProgressBar extends View {


    private String TAG = "MyProgressBar";

    private Paint mBgPaint;
    private Paint mProgressPaint;
    private Paint mTextPaint;

    private int mProgressColor;
    private int mProgressWidth;

    private int mProgress = 0;

    public MyProgressBar(Context context) {
        this(context, null, 0);
    }

    public MyProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TextView, defStyleAttr, 0);
        mProgressColor = typedArray.getColor(R.styleable.TextView_progressColor, Color.RED);
        mProgressWidth = typedArray.getDimensionPixelOffset(R.styleable.TextView_progressWidth, 12);
        typedArray.recycle();

        mBgPaint = new Paint();
        mBgPaint.setColor(Color.BLUE);
        mBgPaint.setStyle(Paint.Style.STROKE);
        mBgPaint.setStrokeWidth(mProgressWidth);

        mProgressPaint = new Paint();
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(mProgressWidth);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(50);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        int left = mProgressWidth;
        int top = mProgressWidth;
        int right = width - mProgressWidth;
        int bottom = height - mProgressWidth;

        //绘制进度背景
        canvas.drawOval(left, top, right, bottom, mBgPaint);

        //绘制进度
        RectF oval = new RectF(left, top, right, bottom);
        canvas.drawArc(oval, 45, (mProgress / 100f) * 360, false, mProgressPaint);

        //绘制文字

        int x = width / 2;
        int y = height / 2;

        //分数
        //计算基线的坐标
        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
        int fontMetricsLeading = fontMetrics.leading;
        int fontMetricsAscent = fontMetrics.ascent;
        int fontMetricsDescent = fontMetrics.descent;
        int fontMetricsTop = fontMetrics.top;
        int fontMetricsBottom = fontMetrics.bottom;
        Log.i(TAG, "leading:" + fontMetricsLeading + ",ascent:" + fontMetricsAscent + ",descent:" + fontMetricsDescent + ",top:" + fontMetricsTop + ",bottom:" + fontMetricsBottom);
        Log.i(TAG, "width:" + getWidth() + ",height:" + getHeight());

        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;
        Log.i(TAG, "dy:" + dy + ", baseLine:" + baseLine);

        //处理padding
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        Log.i(TAG, "paddingLeft:" + paddingLeft + ",paddingRight:" + paddingRight + ",paddingTop:" + paddingTop + ",paddingBottom:" + paddingBottom);

        String startScoreString = String.valueOf(mProgress);

        //计算文字宽度
        Rect bounds = new Rect();
        mTextPaint.getTextBounds(startScoreString, 0, startScoreString.length(), bounds);
        int widthMeasureSpecSize = bounds.width() / 2;

        canvas.drawText(startScoreString, x - widthMeasureSpecSize, baseLine, mTextPaint);

    }

    public void setmProgress(int mProgress) {
        this.mProgress = mProgress;
        invalidate();
    }

}